package com.hyjr.common.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具类
 * 
 * @author huhuamin
 * 
 * @version V1.0 2015-6-3
 * 
 */
public class Utils {

	/**
	 * 生产字母数字随机数
	 * 
	 * @param length
	 * @return
	 */
	public static String getCharAndNumr(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

			if ("char".equalsIgnoreCase(charOrNum)) // 字符串
			{
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) // 数字
			{
				val += String.valueOf(random.nextInt(10));
			}
		}
		return "H" + val;
	}

	public static String generatorId() {
		return String.valueOf(System.currentTimeMillis());
	}

	/**
	 * 实体名转换成数据库名 A_B_C格式
	 * 
	 * @param propertyStr
	 * @return
	 */
	public static String toDBName(String propertyStr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < propertyStr.length(); i++) {
			char a = propertyStr.charAt(i);
			if (a > 64 && a < 91) { // 大写字母的ASCLL码取值范围
				sb.append("_").append(a);
			} else {
				sb.append(String.valueOf(a).toUpperCase());
			}
		}
		return sb.toString();
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	// 16位uuid
	public static String getOrderIdByUUId() {
		int machineId = 1;// 最大支持1-9个集群机器部署
		int hashCodeV = UUID.randomUUID().toString().hashCode();
		if (hashCodeV < 0) {// 有可能是负数
			hashCodeV = -hashCodeV;
		}
		// 0 代表前面补充0
		// 15 代表长度为15
		// d 代表参数为正数型
		return machineId + String.format("%015d", hashCodeV);
	}

	public static Date formatJsDate(String date) {
		date = date.replace("一月", "01").replace("二月", "02").replace("三月", "03")//
				.replace("四月", "04").replace("五月", "05").replace("六月", "06").//
				replace("七月", "07").replace("八月", "08").replace("九月", "09").//
				replace("十月", "10").replace("十一月", "11").replace("九月", "12");
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(date);
		} catch (ParseException e) {
		}
		return null;
		// ;

	}

	/**
	 * 将requst传递的key,value转为map
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, String> requestToMap(HttpServletRequest request) {
		@SuppressWarnings("rawtypes")
		Enumeration e = request.getParameterNames();
		Object object = null;
		String paramKey = "";
		Map<String, String> maps = new HashMap<String, String>();
		while (e.hasMoreElements()) {
			object = (Object) e.nextElement();
			paramKey = object.toString().trim();

			maps.put(object.toString().trim(), request.getParameter(paramKey));
		}
		return maps;
	}

	/**
	 * 使用 Map按key进行排序
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, String> sortMapByKey(Map<String, String> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
		sortMap.putAll(map);
		return sortMap;
	}

	// 将double 类型的数字格式化保留2位小数点
	public static final String saveTwoPoint(Double doub) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(doub);
	}

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
			"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

	/**
	 * 订单号的生成
	 * 
	 * @return
	 */
	public synchronized static String generateShortUuidPF15() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		SimpleDateFormat format = new SimpleDateFormat("MMddmmss");
		String txnssn = "HYCG1".concat(format.format(new Date())).concat(shortBuffer.toString().substring(0, 7));
		return txnssn;

	}

	public static String generateShort22() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String txnssn = format.format(new Date()).concat(shortBuffer.toString());
		return txnssn;

	}

	public static String generateShort20() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		String txnssn = format.format(new Date()).concat(shortBuffer.toString());
		return txnssn;

	}
	public static void main(String[] args) {
		
		System.out.println(generateShortUuidPF15());
	}

}


// 比较器类
class MapKeyComparator implements Comparator<String> {
	public int compare(String i1, String i2) {
		return i1.compareTo(i2);
	}
}
